<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12">
		<div class="col-sm-2" style="padding-top:5px">
			<label for="deliver_serial_id" class="control-label">发货单流水号：</label>
		</div>
		<div class="col-sm-2">
			<input id="deliver_serial_id" class="form-control"></input>
		</div>
		<div class="col-sm-2" style="padding-left:70px;padding-top:5px">
			<label for="order_serial_id" class="control-label">订单号：</label>
		</div>
		<div class="col-sm-2">
			<input id="order_serial_id" class="form-control"></input>
		</div>
		<div class="col-sm-2" style="padding-left:70px;padding-top:5px">
			<label for="receivename" class="control-label">收货人：</label>
		</div>
		<div class="col-sm-2">
      <input id="receivename" class="form-control"></input>
    </div>
	</div>
	<div class="col-sm-12">
		<br>
	<table id="order_list_table" class="table table-hover table-bordered table-striped table-striped">
		<thead>
			<tr>
				<td>发货单流水号</td>
				<td>订单号</td>
				<td>下单时间</td>
				<td>收货人</td>
				<td>发货时间</td>
				<td>总金额</td>
				<td>发货仓库</td>
				<td>应付金额</td>
				<td>操作人</td>
				<td>操作</td>
				<td>order_id</td>
			</tr>
		</thead>
		<tbody>

		</tbody>
	</table>
</div>
</div>

<include file='Index/footer' />
<include file='Order/deliver-detail' />
<style>
tr.selected {
    background-color: #B0BED9;
}
</style>
<script>
var order_id;
$(document).ready(function() {
		loadTable();
});
var table;
var selectedRow;
function loadTable(){
	table = $('#order_list_table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"order": [[ 4, "desc" ]],
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          },
		"ajax":'{:U("Admin/Order/getDeliver/")}',
		"columns": [
					{ "data": 'deliver_serial_id' },
					{ "data": 'order_serial_id' },
					{ "data": 'createtime' },
	        { "data": 'receivename' },
	        { "data": 'createtime' },
	        { "data": 'price' },
	        { "data": 'repository' },
	        { "data": 'realprice' },
	        { "data": 'admin_name' },
	        { "data": 'btn' },
	        { "data": 'order_id' }
    ],
    "createdRow": function ( row, data, index ) {
    	if(data.isprint == "1"){
    		$('td', row).eq(1).parent().addClass('selected');
    	}
    },
    "columnDefs": [
    {
			"targets": 0,
			"render": function (data, type, row, table) {
				var retId = '';
				if(row.a_repository_name){
					retId = retId + data + '-a' + '<br>';
				}
				if(row.b_repository_name){
					retId = retId + data + '-b';
				}
				return retId;
      }
    },
    {
			"targets": 3,
			"render": function (data, type, row, table) {
        return row.receivename + '<br>' + row.phone + '<br>' + row.address;
      }
    },
    {
			"targets": [5, 7],
			"render": function (data, type, row, table) {
        return (parseFloat(data) + parseFloat(row.sendprice)).toFixed(2) + '元';
      }
    },
    {
			"targets": 6,
			"render": function (data, type, row, table) {
        return (row.a_repository_name?row.a_repository_name:"") + '<br>' + (row.b_repository_name?row.b_repository_name:"");
      }
    },
    {
    	"targets": -2,
    	"data": null,
    	"render": function (data, type, row, table) {
    		var btn = '';
    		if(row.a_repository_name){
    			btn += '<button class="btn btn-info" onclick="view(this, \''+row.order_id+'\', \'A\', true)">A发货单</button><br><br>'
    		}
    		if(row.b_repository_name){
					btn += '<button class="btn btn-info" onclick="view(this, \''+row.order_id+'\', \'B\')">B发货单</button>';
    		}
        return btn;
      },
    },
    {
			"targets": -1,
			"visible": false
    }]
	});

	$("#deliver_serial_id").keyup(function(){
		table.column(0).search(this.value).draw();
  });

	$("#order_serial_id").keyup(function(){
		table.column(1).search(this.value).draw();
  });

  $("#receivename").keyup(function(){
		table.column(3).search(this.value).draw();
  });
}

function view(obj, order_id, deliver_type, hasA){
	window.order_id = order_id;
	window.selectedRow = $(obj).parent().parent();
	//如果没有传值，hasA为false
	if(!arguments[2])
		flag = false;

	$.ajax({
			url: '{:U("Admin/Order/getDeliver/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"order_id":order_id
			}
		})
		.success(function(data){
			switch(data.status){
				case '0':
					$('#status').html('已发货');
					break;
				case '1':
					$('#status').html('已付款');
					break;
				case '2':
					$('#status').html('已完成');
					break;
				case '3':
					$('#status').html('待退货');
					break;
				case '4':
					$('#status').html('已退货');
					break;
			}

			$('#createtime1').html(data.createtime);
			$('#createtime2').html(data.createtime);
			$('#username').html(data.user.name);
			$('#price').html((parseFloat(data.price)+parseFloat(data.sendprice)).toFixed(2) + "元");
			$('#realprice').html((parseFloat(data.realprice)+parseFloat(data.sendprice)).toFixed(2) + "元");
			$('#receivename_show').html(data.receivename);
			$('#address').html(data.address);
			$('#phone').html(data.phone);

			var sendTime = 39;
			var products = data.products;
			var product_html = '', print_product = '';
			var product_num = 0;
			var product_amount = 0;
			var product_realprice = 0;
			var id = 0;
			for (var i = 0; i < products.length; i++) {
				if(deliver_type == 'A' && (products[i].b_repository_id == null || products[i].b_repository_id == 0)){
					//如果商品sendtype!=0，选择新的sentime
					if(products[i].sendtype != '0'){
						if(products[i].sendtype == '1'){
							sendTime = Math.max(sendTime, 49);
						}else if(products[i].sendtype == '2'){
							sendTime = Math.max(sendTime, 59);
						}
					}
					$('#order_serial_id_show').html(data.order_serial_id + '-a');
					$('#print_order_serial_id').html(data.order_serial_id + '-a');

					id++;
					print_product += '<tr>'+
											'<td>'+formatString(transNull(products[i].name),5)+'<br>'+transNull(products[i].scale)+'</td>'+
                			'<td><br>'+transNull(products[i].num)+'</td>'+
                			'<td><br>'+transNull(parseFloat(products[i].realprice).toFixed(2))+'</td>'+
                			'<td><br>'+(products[i].realprice*products[i].num).toFixed(2)+'</td></tr>';

					product_html += '<tr>'+
                			'<td>'+transNull(products[i].name)+'</td>'+
                			'<td>'+transNull(products[i].barcode)+'</td>'+
                			'<td>'+transNull(products[i].kj_price)+'</td>'+
                			'<td>'+transNull(products[i].num)+'</td>'+
                			'<td>'+transNull(products[i].origin_place)+'</td>'+
                			'<td>'+transNull(products[i].default_amount)+'</td>'+
                			'<td>'+(products[i].kj_price*products[i].num).toFixed(2)+'</td></tr>';
          product_num += parseInt(products[i].num);
          product_amount += products[i].kj_price*products[i].num;
          product_realprice += products[i].realprice*products[i].num;
        }

        if(deliver_type == 'B' && (products[i].a_repository_id == null|| products[i].a_repository_id == '0')){
        	sendTime = 1*24*60;

					$('#order_serial_id_show').html(data.order_serial_id + '-b');
					$('#print_order_serial_id').html(data.order_serial_id + '-b');

					id++;
					print_product += '<tr>'+
											'<td>'+formatString(transNull(products[i].name),5)+'<br>'+transNull(products[i].scale)+'</td>'+
                			'<td><br>'+transNull(products[i].num)+'</td>'+
                			'<td><br>'+transNull(parseFloat(products[i].realprice).toFixed(2))+'</td>'+
                			'<td><br>'+(products[i].realprice*products[i].num).toFixed(2)+'</td></tr>';

					product_html += '<tr>'+
                			'<td>'+transNull(products[i].name)+'</td>'+
                			'<td>'+transNull(products[i].barcode)+'</td>'+
                			'<td>'+transNull(products[i].kj_price)+'</td>'+
                			'<td>'+transNull(products[i].num)+'</td>'+
                			'<td>'+transNull(products[i].origin_place)+'</td>'+
                			'<td>'+transNull(products[i].default_amount)+'</td>'+
                			'<td>'+(products[i].kj_price*products[i].num).toFixed(2)+'</td></tr>';
          product_num += parseInt(products[i].num);
          product_amount += products[i].kj_price*products[i].num;
          product_realprice += products[i].realprice*products[i].num;
        }
			}
			//收货时间的计算
			if(data.type == '0'){
				$('#receivetime').html(addDate(data.createtime, sendTime));
			}else{
				$('#receivetime').html(addDate(data.createtime, 1*24*60));
			}

			product_html += '<tr><td></td><td></td><td></td><td></td><td></td><td>合计</td><td>'+product_amount.toFixed(2)+'</td></tr>';
			$('#product').html(product_html);

			var product_price = "￥"+product_amount.toFixed(2)+" - 折扣：￥"+(product_amount-product_realprice).toFixed(2);
			var product_order_realprice = (parseFloat(product_realprice)).toFixed(2);
			if((hasA && deliver_type == 'A') || (!hasA && deliver_type == 'B')){
				product_price = "￥"+product_amount.toFixed(2)+" - 折扣：￥"+(product_amount-product_realprice).toFixed(2)+" +  配送费：￥"+parseFloat(data.sendprice).toFixed(2);
				product_order_realprice = (parseFloat(product_realprice)+parseFloat(data.sendprice)).toFixed(2);
			}
			$('#product_price').html(product_price);
			$('#product_realprice').html("￥"+product_order_realprice);

			$('#print_btn').attr('onclick', 'print(\''+order_id+'\')');

			//打印页面
			$('#print_product').html(print_product);
			$('#print_product_num').html(product_num+"件");
			$('#print_realprice').html(product_order_realprice+'元');
			if((hasA && deliver_type == 'A') || (!hasA && deliver_type == 'B')){
				$('#print_sendprice').html(data.sendprice+'元');
			}else{
				$('#print_sendprice').html('0.00元');
			}

			$('#print_createtime1').html(data.createtime);
			$('#print_receivename').html(data.receivename);
			$('#print_phone').html(data.phone);
			$('#print_address').html(data.address);

			$('#deliver_detail_modal').modal({
				backdrop : "static"
			});
	});
}

</script>
